package com.yzz.composeweatherproject.activity.launched_effect

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay

/**
 * @Author : yangzuozhe
 * @Time : On 2022/5/31 23:01
 *
 * @Description : LaunchedEffect
 */
@Composable
fun Greeting(name: String) {
    var state by remember {
        mutableStateOf(1)
    }
    var resp by remember {
        mutableStateOf("hello $name!")
    }
    LaunchedEffect(state) {
        delay(400)
        val stateN = if (state % 2 == 0) {
            "我是偶数"
        } else {
            "我是奇数"
        }
        resp = "$stateN,state:${state}\n这个block执行在协程${Thread.currentThread().name}中"
        Log.d("yyzzresp",resp)
    }
    Column {
        Text(text = resp)
        Button(
            onClick = { ++state },
            modifier = Modifier
                .height(50.dp)
                .width(100.dp)
        ) {
            Text(text = "点一点")
        }
    }
}

@Composable
fun Greeting2(name: String) {
    Text(text = "Hello $name!")
    LaunchedEffect(null) {
        Log.d("yyzztag", "这个block执行在协程${Thread.currentThread().name}中")
    }
}